<%--
  User: juventus
  Date: 11-3-18
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
    response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
    response.setHeader("Pragma","no-cache"); //HTTP 1.0
    response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>用户管理</title>
    <link rel="stylesheet" type="text/css" href="/js/easyui/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="/js/easyui/themes/icon.css">
    <script type="text/javascript" src="/js/easyui/jquery-1.7.1.min.js"></script>
    <script type="text/javascript" src="/js/easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/js/easyui/locale/easyui-lang-zh_CN.js"></script>

    <script>
        $(function() {
            $('#adminGrid').datagrid({
                title:'用户列表',
                iconCls:'icon-save',
                width:"100%",
                height:400,
                nowrap: false,
                striped: true,
                collapsible:true,
                url:'/management/fetchAdministrators.action',
                queryParams:{},
                remoteSort: false,
                idField:'id',
                columns:[
                    [
                        {"width":120,"field":"name","title":"登录名"},
                        {"width":200,"field":"displayName","title":"显示名称"},
                        {"width":80,"field":"lastLoginIP","title":"上次登录ip"},
                        {"width":200,"field":"lastLoginDate","title":"上次登录日期"},
                        {"width":80,"field":"role","title":"角色",
                            formatter:function(value, rowData, rowIndex) {
                                if (value == '1') {
                                    return '管理员';
                                }else if(value == '0'){
                                    return '新闻编辑';
                                }else if(value == '2'){
                                    return '资深编辑';
                                }else if(value == '3'){
                                    return '实习编辑';
                                }else if(value == '4'){
                                    return '兼职编辑';
                                }else{
                                    return 'Unknown user role';
                                }
                            }
                        },
                        {"width":200,"field":"createDate","title":"创建日期"},
                        {"width":80,"field":"id","title":"操作",
                            formatter:function(value, rowData, rowIndex) {
                                return '<a href="#" onclick="editUser(\'' + rowIndex + '\');">编辑</a>&nbsp;<a onclick="deleteUser(\'' + rowIndex + '\');" href="#">删除</a>';
                            }
                        }
                    ]
                ],
                pagination:true,
                rownumbers:true,
                toolbar:[
                    {"iconCls":"icon-add","handler":function() {
                        openCreateUser();
                    },"text":"添加用户","title":"添加用户","id":"btnAdd"}
                ]
            });

            var p = $('#adminGrid').datagrid('getPager');
            if (p) {
                $(p).pagination({
                    onBeforeRefresh:function() {
                        //	alert('before refresh');
                    }
                });
            }
        });

        function openCreateUser() {
            $('#userForm').form('clear');

            $('#itemEditorDialog').show();
            $('#name').show();
            $('#password').show();
            $('#password_repeat').show();

            $('#itemEditorDialog').dialog({
                title:'添加用户',
                modal:true
            });
        }

        function closeUserForm() {
            $('#userForm').form('clear');
            $('#itemEditorDialog').dialog('close');
        }

        function createUser() {
            if($('#password').attr('value') != $('#password_repeat').attr('value')){
                $.messager.show({
                    title:'提示信息',
                    msg:'密码不一致！'
                });
                return;
            }


            $("#userForm").form('submit', {
                url: "/management/userForm.action",
                onSubmit:function() {
                    return $(this).form('validate');
                },
                success:function(data) {
                    //   alert(data);
                    data = eval('(' + data + ')');
                    var administrator = data.administrator;

                    if (!data.successful) {
                        alert('not true');
                        return;
                    }

                    $.messager.show({
                        title:'提示信息',
                        msg:'保存成功！'
                    });

                    $("#adminGrid").datagrid('reload');
                    closeUserForm();
                }
            });
        }

        function editUser(index) {
            $("#adminGrid").datagrid('selectRow', index);

            var selected = $('#adminGrid').datagrid('getSelected');
            if (selected) {
                $('#userForm').form('load', {
                    'userForm.displayName':selected.displayName,
                    'userForm.id':selected.id,
                    'userForm.name':selected.name,
                    'userForm.password':selected.password,
                    'userForm.lastLoginIP':selected.lastLoginIP,
                    'userForm.lastLoginDate':selected.lastLoginDate,
                    'userForm.role':selected.role,
                    'userForm.createDate':selected.createDate
                });

                $('#userRole').combobox('setValue', selected.role);
                $('#name').hide();
                $('#password').hide();
                $('#password_repeat').hide();
            }

            $('#itemEditorDialog').show();
            $('#itemEditorDialog').dialog({
                title:'编辑用户',
                modal:true
            });
        }

        function deleteUser(index) {
            $("#adminGrid").datagrid('selectRow', index);

            var node = $('#adminGrid').datagrid('getSelected');
            if (!node) {
               return;
            }

             $.messager.confirm('删除用户', '你确定要删除用户吗？', function(data) {
                    if (!data) {
                        return;
                    }

                    $.getJSON("/management/deleteAdminUser.action?userId=" + node.id, function(data) {
                        if (data.successful) {
                            $.messager.show({
                                title:'提示信息',
                                msg:'删除成功！'
                            });

                            $("#adminGrid").datagrid('reload');
                        }else{
                            $.messager.show({
                                title:'提示信息',
                                msg:'删除失败！原因：'+data.errorMessage
                            });
                        }
                    });
                });
        }
    </script>

</head>
<body>
<s:if test="%{#session._login_user_==null}">
    <script type="text/javascript">
        top.location.href = '/admin';
    </script>
</s:if>
<table id="adminGrid"></table>

<div id="itemEditorDialog" icon="icon-save" title="用户管理" style="height:400;width:400;display:none;padding:5px;">
    <form method="post" id="userForm" name="userForm">
        <table>
            <tr>
                <td><label>登录名：</label></td>
                <td><input class="easyui-validatebox" id="name" name="userForm.name" required="true"/></td>
            </tr>
            <tr>
                <td><label>密码：</label></td>
                <td><input class="easyui-validatebox" id="password" type="password" name="userForm.password"
                           required="true"/></td>
            </tr>
            <tr>
                <td><label>重复密码：</label></td>
                <td><input class="easyui-validatebox" id="password_repeat" type="password" name="passwordRepeat"
                           required="true"/></td>
            </tr>
            <tr>
                <td><label>显示名称：</label></td>
                <td><input class="easyui-validatebox" id="displayName" name="userForm.displayName"/></td>
            </tr>
            <tr>
                <td><label>角色：</label></td>
                <td>
                    <select id="userRole" class="easyui-combobox" name="userForm.role" style="width:160px;"
                            required="true">
                        <option value="0">编辑</option>
                        <option value="1">管理员</option>
                        <option value="2">资深编辑</option>
                        <option value="3">实习编辑</option>
                        <option value="4">兼职编辑</option>
                    </select>
                </td>
            </tr>
        </table>
        <div class="toolbar" style="padding:10px;">
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <a href="#" onclick="createUser()" class="easyui-linkbutton" iconCls="icon-ok">确定</a>
            <a href="#" onclick="closeUserForm()" class="easyui-linkbutton" iconCls="icon-cancel">取消</a>
        </div>
        <input type="hidden" name="userForm.createDate"
               value="<s:date name='userForm.createDate' format='yyyy-MM-dd HH:mm:ss'/>"/>
        <input type="hidden" name="userForm.id"/>
        <input type="hidden" name="userForm.lastLoginDate"
               value="<s:date name='userForm.lastLoginDate' format='yyyy-MM-dd HH:mm:ss'/>"/>
        <input type="hidden" name="userForm.lastLoginIP"/>
    </form>
</div>

</body>
</html>